home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1250 / BCPP18.ZIP / BCPP / CODE / BASEQ.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-20  |  6.4 KB  |  216 lines

  1. #ifndef _QUEUE_LIST_CODE
  2. #define _QUEUE_LIST_CODE
  3.  
  4. // Code written by Steven De Toni ACBC 11
  5.  
  6. // These class methods used to implement a object that holds other objects
  7. // that are decendant of ANYOBJECT (i.e universal container).
  8.  
  9. #include "baseq.h"
  10. #include <stdio.h>      // NULL CONSTANT
  11.  
  12.  
  13. // ############################################################################
  14. // #### QueueList Class ####
  15. // #########################
  16.  
  17. // ############################ Protected Methods #############################
  18.  
  19. // Create new list item and place programmer's data within it,
  20. // make new links with previous item if wished.
  21. // returns a pointer to the new item, NULL if operation failed.
  22. //
  23. // Parameters:
  24. //     pItem    : Pointer to the object to be stored.
  25. //     LinkItem : Pointer to end of list where item to be added.
  26. //
  27. // Return Values:
  28. //     LinkItem : returns pointer to newly added item in list,
  29. //                NULL if operation failed. However items within list
  30. //                before hand still exist.  
  31. LinkItem* QueueList::newItem (ANYOBJECT* pItem, LinkItem* pEndList)
  32. {
  33.     LinkItem*  pNewStruct = new LinkItem;
  34.  
  35.     if (pNewStruct != NULL)
  36.     {
  37.         pNewStruct->pLinkedItem    = pEndList;
  38.         pNewStruct->pStoredItem    = pItem;
  39.     }
  40.  
  41.     return pNewStruct;
  42. }
  43.  
  44.  
  45. // ############################## Public Methods ##############################
  46. // ############################### Constructors ###############################
  47. QueueList::QueueList       (void)
  48. {
  49.    itemCount      = 0;
  50.    spaceAvailable = 0;
  51.    pEndPos        = NULL;
  52. }
  53.  
  54. // Parameters:
  55. //     pItem    : Pointer to a object to be stored, must be decendant of
  56. //                base class ANYOBJECT.
  57. QueueList::QueueList       (ANYOBJECT* pItem)
  58. {
  59.    itemCount      = 0;
  60.    spaceAvailable = 0;
  61.    pEndPos        = NULL;
  62.    putLast(pItem);
  63. }
  64.  
  65. // ########################### User Methods ###################################
  66.  
  67. // Place programmers object into list
  68. //
  69. // Parameters:
  70. //     pItem    : Pointer to a object to be stored, must be decendant of
  71. //                base class ANYOBJECT.
  72. //
  73. // Return Values:
  74. //     int      : Returns a error code indicating whether operation was
  75. //                successful.
  76. //                Values:
  77. //                    0 = No Worries
  78. //                   -1 = Arrgh ... No memory
  79. //                        
  80. int       QueueList::putLast   (ANYOBJECT* pItem)
  81. {
  82.     LinkItem*   pNewItem = newItem (pItem, pEndPos);
  83.     if (pNewItem != NULL)
  84.     {
  85.         pEndPos = pNewItem;
  86.         itemCount++;
  87.         return 0;
  88.     }
  89.     spaceAvailable = -1;
  90.     return -1;          // could not add item to list!
  91. }
  92.  
  93.  
  94. // Take first item placed in Queue, out and return it. 
  95. // Type casting is required to return object back to it's orginial
  96. // state.
  97. //
  98. // Return Values:
  99. //     ANYOBJECT* : Pointer to the object that was stored within queue.
  100. //
  101. ANYOBJECT*   QueueList::takeNext         (void)
  102. {
  103.       if (pEndPos != NULL)
  104.       {
  105.         LinkItem* pUpDateList = pEndPos;
  106.         LinkItem* pStartPos   = pEndPos;
  107.  
  108.         // move down list until start has been reached
  109.         while (pStartPos->pLinkedItem != NULL)
  110.               pStartPos = pStartPos->pLinkedItem;
  111.  
  112.         if (pStartPos != pUpDateList) // if not the last item in list
  113.         {
  114.             // retrieve data and delete item from list
  115.             while (pUpDateList->pLinkedItem != pStartPos)
  116.                   pUpDateList = pUpDateList->pLinkedItem;
  117.         }
  118.         else
  119.         {
  120.             pEndPos = NULL;             // start new list after all items gone
  121.         }
  122.  
  123.         ANYOBJECT* pTemp = pStartPos->pStoredItem;  // copy value to user
  124.         pUpDateList->pLinkedItem = NULL;            // make new start of list
  125.         delete pStartPos;                           // delete object
  126.         itemCount--;                                // one less
  127.         if (spaceAvailable)                         // if no memory available before...
  128.                 spaceAvailable = 0;     // there is now!
  129.         return pTemp;
  130.       }
  131.       else
  132.           return NULL;
  133. }
  134.  
  135. // Returns the number of items contained within the queue.
  136. //
  137. // Returns Values:
  138. //     int :    Num of items within queue.
  139. //
  140. int      QueueList::status (void)    // return number of item in Queue
  141. {
  142.         return itemCount;
  143. }
  144.  
  145. // Method returns whether last operation failed due to memory allocation
  146. // failure.
  147. //
  148. // Return Values:
  149. //     int  : Returns 1 of two values ...
  150. //            Values:
  151. //              0  =  memory available
  152. //             -1  =  Last memory allocation failed.
  153. //       
  154. int      QueueList::space  (void)     // return Queue space left
  155. {
  156.         return spaceAvailable;       // return -1 if no space available
  157. }
  158.  
  159. // Methods is used to peek within the queue at objects, and return there 
  160. // pointer without taking them out of the queue.
  161. //
  162. // Parameters:
  163. //     NumFromNext : The object number to look at from the start of the 
  164. //                   queue. The start of the queue is 1, not 0.
  165. //   
  166. // Return Values:
  167. //     ANYOBJECT* : Pointer to the object that is stored within queue,
  168. //                  at said position. Returns NULL if operation failed.
  169. //
  170. ANYOBJECT*   QueueList::peek (int numFromNext)
  171. {
  172.     if (pEndPos != NULL)
  173.     {
  174.         //if (numFromNext > itemCount) //error checking !
  175.         //   return NULL;
  176.  
  177.         int count = itemCount - numFromNext;
  178.         LinkItem* pStartPos   = pEndPos;
  179.  
  180.         // move down list until start has been reached
  181.         while (count > 0)
  182.         {
  183.               pStartPos = pStartPos->pLinkedItem;
  184.               count--;
  185.         }
  186.  
  187.         if (pStartPos != NULL)
  188.             return pStartPos->pStoredItem;
  189.         else
  190.             return NULL;
  191.     }
  192.     else
  193.           return NULL;
  194. }
  195.  
  196.  
  197.  
  198. // ############################### Destructor ###############################
  199. // Method will remove all list items from memory if they still exist,
  200. // no garabage collection provided, or used.
  201. //
  202. QueueList::~QueueList      (void)
  203. {
  204.         LinkItem* pTemp = pEndPos;
  205.  
  206.         while (pEndPos != NULL)
  207.         {
  208.               pEndPos = pEndPos->pLinkedItem;   // advance to next item
  209.               delete    pTemp  ->pStoredItem;     // kill data contained
  210.               delete    pTemp;                  // kill item
  211.               pTemp   = pEndPos;
  212.         }
  213. }
  214.  
  215. #endif
  216.